package cn.ultrarisk.scadmin.manager.biz.solr.entity;

import cn.ultrarisk.mybatis.extension.annotation.Table;
import cn.ultrarisk.scadmin.manager.util.StringUtil;

import java.io.Serializable;
import java.util.*;


/**
 * solr节点实体类
 *
 * @author sevendlong(lin23871@163.com)
 * @date 2014/4/14
 * @since V1.0
 */
@Table(table = "solr_node", idColumn = SolrNode.ID, autoIncrement = true)
public class SolrNode implements Serializable {
    private static final long serialVersionUID = -4432339314817798951L;

    public final static String ID = "id";
    public final static String NAME = "name";
    public final static String HOST = "host";
    public final static String PORT = "port";
    public final static String PATH = "path";
    public final static String AVAILABLE = "available";
    public final static String SOLR_SPEC_VERSION = "solr_spec_version";
    public final static String SOLR_IMPL_VERSION = "solr_impl_version";
    public final static String LUCENE_SPEC_VERSION = "lucene_spec_version";
    public final static String LUCENE_IMPL_VERSION = "lucene_impl_version";
    public final static String JVM_VERSION = "jvm_version";
    public final static String JVM_NAME = "jvm_name";
    public final static String JVM_SPEC_VENDOR = "jvm_spec_vendor";
    public final static String JVM_SPEC_NAME = "jvm_spec_name";
    public final static String JVM_SPEC_VERSION = "jvm_spec_version";
    public final static String JVM_JRE_VENDOR = "jvm_jre_vendor";
    public final static String JVM_JRE_VERSION = "jvm_jre_version";
    public final static String JVM_VM_VENDOR = "jvm_vm_vendor";
    public final static String JVM_VM_NAME = "jvm_vm_name";
    public final static String JVM_VM_VERSION = "jvm_vm_version";
    public final static String JVM_PROCESSORS = "jvm_processors";
    public final static String JVM_START_TIME = "jvm_start_time";
    public final static String JVM_UPTIME = "jvm_uptime";
    public final static String SYSTEM_NAME = "system_name";
    public final static String SYSTEM_VERSION = "system_version";
    public final static String SYSTEM_ARCH = "system_arch";
    public final static String SYSTEM_UNAME = "system_uname";
    public final static String SYSTEM_UPTIME = "system_uptime";
    public final static String UPDATE_TIME = "update_time";

    public static final Map<String, String> COLUMN_PROPERTY_MAP = new LinkedHashMap<String, String>();

    static {
        COLUMN_PROPERTY_MAP.put(ID, "id");
        COLUMN_PROPERTY_MAP.put(NAME, "name");
        COLUMN_PROPERTY_MAP.put(HOST, "host");
        COLUMN_PROPERTY_MAP.put(PORT, "port");
        COLUMN_PROPERTY_MAP.put(PATH, "path");
        COLUMN_PROPERTY_MAP.put(AVAILABLE, "available");
        COLUMN_PROPERTY_MAP.put(SOLR_SPEC_VERSION, "solrSpecVersion");
        COLUMN_PROPERTY_MAP.put(SOLR_IMPL_VERSION, "solrImplVersion");
        COLUMN_PROPERTY_MAP.put(LUCENE_SPEC_VERSION, "luceneSpecVersion");
        COLUMN_PROPERTY_MAP.put(LUCENE_IMPL_VERSION, "luceneImplVersion");
        COLUMN_PROPERTY_MAP.put(JVM_VERSION, "jvmVersion");
        COLUMN_PROPERTY_MAP.put(JVM_NAME, "jvmName");
        COLUMN_PROPERTY_MAP.put(JVM_SPEC_VENDOR, "jvmSpecVendor");
        COLUMN_PROPERTY_MAP.put(JVM_SPEC_NAME, "jvmSpecName");
        COLUMN_PROPERTY_MAP.put(JVM_SPEC_VERSION, "jvmSpecVersion");
        COLUMN_PROPERTY_MAP.put(JVM_JRE_VENDOR, "jvmJreVendor");
        COLUMN_PROPERTY_MAP.put(JVM_JRE_VERSION, "jvmJreVersion");
        COLUMN_PROPERTY_MAP.put(JVM_VM_VENDOR, "jvmVmVendor");
        COLUMN_PROPERTY_MAP.put(JVM_VM_NAME, "jvmVmName");
        COLUMN_PROPERTY_MAP.put(JVM_VM_VERSION, "jvmVmVersion");
        COLUMN_PROPERTY_MAP.put(JVM_PROCESSORS, "jvmProcessors");
        COLUMN_PROPERTY_MAP.put(JVM_START_TIME, "jvmStartTime");
        COLUMN_PROPERTY_MAP.put(JVM_UPTIME, "jvmUptime");
        COLUMN_PROPERTY_MAP.put(SYSTEM_NAME, "systemName");
        COLUMN_PROPERTY_MAP.put(SYSTEM_VERSION, "systemVersion");
        COLUMN_PROPERTY_MAP.put(SYSTEM_ARCH, "systemArch");
        COLUMN_PROPERTY_MAP.put(SYSTEM_UNAME, "systemUname");
        COLUMN_PROPERTY_MAP.put(SYSTEM_UPTIME, "systemUptime");
        COLUMN_PROPERTY_MAP.put(UPDATE_TIME, "updateTime");
    }

    /**
     * 编号
     */
    private Integer id;
    /**
     * 名称
     */
    private String name;
    /**
     * 主机
     */
    private String host;
    /**
     * 端口
     */
    private Integer port;
    /**
     * 路径
     */
    private String path;
    /**
     * 是否有效
     */
    private Boolean available;
    /**
     * solr规范版本
     */
    private String solrSpecVersion;
    /**
     * solr 实现版本
     */
    private String solrImplVersion;
    /**
     * lucene规范版本
     */
    private String luceneSpecVersion;
    /**
     * lucene实现版本
     */
    private String luceneImplVersion;
    /**
     * jvm版本
     */
    private String jvmVersion;
    /**
     * jvm名称
     */
    private String jvmName;
    /**
     * javaapi规范厂商
     */
    private String jvmSpecVendor;
    /**
     * javaapi规范名称
     */
    private String jvmSpecName;
    /**
     * javaapi规范版本
     */
    private String jvmSpecVersion;
    /**
     * jre厂商
     */
    private String jvmJreVendor;
    /**
     * jre版本
     */
    private String jvmJreVersion;
    /**
     * java虚拟机厂商
     */
    private String jvmVmVendor;
    /**
     * java虚拟机名称
     */
    private String jvmVmName;
    /**
     * java虚拟机版本
     */
    private String jvmVmVersion;
    /**
     * jvm处理器数量
     */
    private Integer jvmProcessors;
    /**
     * jvm启动时间
     */
    private Date jvmStartTime;
    /**
     * jvm运行时间
     */
    private Long jvmUptime;
    /**
     * 操作系统名称
     */
    private String systemName;
    /**
     * 操作系统版本
     */
    private String systemVersion;
    /**
     * 操作系统体系
     */
    private String systemArch;
    /**
     * 操作系统详细信息
     */
    private String systemUname;
    /**
     * 操作系统运行时间
     */
    private String systemUptime;
    /**
     * 更新时间
     */
    private Long updateTime;
    /**
     * 副本
     */
    private List<SolrReplica> replicas;

    public SolrNode() {
    }

    public SolrNode(String name) {
        this.name = name;
        String[] arr = this.name.split("[:_]");
        this.host = arr[0];
        this.port = Integer.parseInt(arr[1]);
        this.path = arr.length == 3 ? arr[2] : "";
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public Integer getPort() {
        return port;
    }

    public void setPort(Integer port) {
        this.port = port;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public Boolean getAvailable() {
        return available;
    }

    public void setAvailable(Boolean available) {
        this.available = available;
    }

    public String getSolrSpecVersion() {
        return solrSpecVersion;
    }

    public void setSolrSpecVersion(String solrSpecVersion) {
        this.solrSpecVersion = solrSpecVersion;
    }

    public String getSolrImplVersion() {
        return solrImplVersion;
    }

    public void setSolrImplVersion(String solrImplVersion) {
        this.solrImplVersion = solrImplVersion;
    }

    public String getLuceneSpecVersion() {
        return luceneSpecVersion;
    }

    public void setLuceneSpecVersion(String luceneSpecVersion) {
        this.luceneSpecVersion = luceneSpecVersion;
    }

    public String getLuceneImplVersion() {
        return luceneImplVersion;
    }

    public void setLuceneImplVersion(String luceneImplVersion) {
        this.luceneImplVersion = luceneImplVersion;
    }

    public String getJvmVersion() {
        return jvmVersion;
    }

    public void setJvmVersion(String jvmVersion) {
        this.jvmVersion = jvmVersion;
    }

    public String getJvmName() {
        return jvmName;
    }

    public void setJvmName(String jvmName) {
        this.jvmName = jvmName;
    }

    public String getJvmSpecVendor() {
        return jvmSpecVendor;
    }

    public void setJvmSpecVendor(String jvmSpecVendor) {
        this.jvmSpecVendor = jvmSpecVendor;
    }

    public String getJvmSpecName() {
        return jvmSpecName;
    }

    public void setJvmSpecName(String jvmSpecName) {
        this.jvmSpecName = jvmSpecName;
    }

    public String getJvmSpecVersion() {
        return jvmSpecVersion;
    }

    public void setJvmSpecVersion(String jvmSpecVersion) {
        this.jvmSpecVersion = jvmSpecVersion;
    }

    public String getJvmJreVendor() {
        return jvmJreVendor;
    }

    public void setJvmJreVendor(String jvmJreVendor) {
        this.jvmJreVendor = jvmJreVendor;
    }

    public String getJvmJreVersion() {
        return jvmJreVersion;
    }

    public void setJvmJreVersion(String jvmJreVersion) {
        this.jvmJreVersion = jvmJreVersion;
    }

    public String getJvmVmVendor() {
        return jvmVmVendor;
    }

    public void setJvmVmVendor(String jvmVmVendor) {
        this.jvmVmVendor = jvmVmVendor;
    }

    public String getJvmVmName() {
        return jvmVmName;
    }

    public void setJvmVmName(String jvmVmName) {
        this.jvmVmName = jvmVmName;
    }

    public String getJvmVmVersion() {
        return jvmVmVersion;
    }

    public void setJvmVmVersion(String jvmVmVersion) {
        this.jvmVmVersion = jvmVmVersion;
    }

    public Integer getJvmProcessors() {
        return jvmProcessors;
    }

    public void setJvmProcessors(Integer jvmProcessors) {
        this.jvmProcessors = jvmProcessors;
    }

    public Date getJvmStartTime() {
        return jvmStartTime;
    }

    public void setJvmStartTime(Date jvmStartTime) {
        this.jvmStartTime = jvmStartTime;
    }

    public Long getJvmUptime() {
        return jvmUptime;
    }

    public void setJvmUptime(Long jvmUptime) {
        this.jvmUptime = jvmUptime;
    }

    public String getSystemName() {
        return systemName;
    }

    public void setSystemName(String systemName) {
        this.systemName = systemName;
    }

    public String getSystemVersion() {
        return systemVersion;
    }

    public void setSystemVersion(String systemVersion) {
        this.systemVersion = systemVersion;
    }

    public String getSystemArch() {
        return systemArch;
    }

    public void setSystemArch(String systemArch) {
        this.systemArch = systemArch;
    }

    public String getSystemUname() {
        return systemUname;
    }

    public void setSystemUname(String systemUname) {
        this.systemUname = systemUname;
    }

    public String getSystemUptime() {
        return systemUptime;
    }

    public void setSystemUptime(String systemUptime) {
        this.systemUptime = systemUptime;
    }

    public Long getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(Long updateTime) {
        this.updateTime = updateTime;
    }

    public List<SolrReplica> getReplicas() {
        return replicas;
    }

    public void setReplicas(List<SolrReplica> replicas) {
        this.replicas = replicas;
    }

    public void addReplica(SolrReplica replica) {
        if (this.replicas == null) {
            this.replicas = new ArrayList<SolrReplica>();
        }
        this.replicas.add(replica);
    }

    public SolrReplica getReplicaByCoreName(String coreName) {
        if (this.replicas != null) {
            for (SolrReplica replica : replicas) {
                if (replica.getCoreName().equals(coreName)) {
                    return replica;
                }
            }
        }
        return null;
    }

    public String getBaseUrl() {
        String url = "http://" + host + ":" + port;
        if (StringUtil.isNotEmpty(path)) {
            url += "/" + path;
        }
        return url;
    }

    public String getMonitorUrl() {
        return this.getBaseUrl() + "/admin/cores?wt=json&action=monitor&t=" + System.currentTimeMillis();
    }
}